﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.News
{
    public class NewsBL
    {
        public void clsNewsInsert(tbl_New newss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_New news = new tbl_New();
                    news.CategoryNo = newss.CategoryNo;
                    news.CategoryName = newss.CategoryName;
                    news.Title = newss.Title;
                    news.ShortDescription = newss.ShortDescription;
                    news.Thumb = newss.Thumb;
                    news.NewsContent = newss.NewsContent;
                    news.IsHotNews = newss.IsHotNews;
                    news.IndexShow = newss.IndexShow;
                    news.ViewCount = newss.ViewCount;
                    news.CreatedDate = DateTime.Now;
                    news.CreatedUser = newss.CreatedUser;
                    news.Status = newss.Status;
                    news.LangId = newss.LangId;

                    adlerContextDb.tbl_News.InsertOnSubmit(news);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsNewsModify(tbl_New newss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var news = adlerContextDb.tbl_News.Single(d => d.NewsNo == newss.NewsNo);
                    news.CategoryNo = newss.CategoryNo;
                    news.CategoryName = newss.CategoryName;
                    news.Title = newss.Title;
                    news.ShortDescription = newss.ShortDescription;
                    news.Thumb = newss.Thumb;
                    news.NewsContent = newss.NewsContent;
                    news.IsHotNews = newss.IsHotNews;
                    news.IndexShow = newss.IndexShow;
                    news.ViewCount = newss.ViewCount;
                    news.Status = newss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsNewsDelete(tbl_New newss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var news = adlerContextDb.tbl_News.Single(d => d.NewsNo == newss.NewsNo);
                    news.Status = newss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show News.
        public List<MODEL_SP_ADMIN_SEARCH_NEWS> Get_Admin_News(string categoryno, string title, string ishotnews, string datefrom, string dateto, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_NEWS(categoryno, title, ishotnews, datefrom, dateto, status)
                //            select new MODEL_SP_ADMIN_SEARCH_NEWS
                //            {
                //                Index = index++,
                //                NewsNo = n.NewsNo,
                //                CategoryNo = n.CategoryNo,
                //                CategoryName = n.CategoryName,
                //                Title = n.Title,
                //                ShortDescription = n.ShortDescription,
                //                Thumb = n.Thumb,
                //                NewsContent = n.NewsContent,
                //                IsHotNews = n.IsHotNews,
                //                IndexShow = n.IndexShow,
                //                ViewCount = n.ViewCount,
                //                CreatedDate = n.CreatedDate,
                //                CreatedUser = n.CreatedUser,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public IEnumerable<tbl_New> GetNews()
        {
            var dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_News;
        }

        public tbl_New GetNewsByNewsno(int newsno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var r= contextdb.tbl_News.FirstOrDefault(x => x.NewsNo == newsno);
                return r;
            }
        }

        public IEnumerable<T> GetDropDownListCategoryNews<T>(Func<string,int,T> creator ) where T:class 
        {
            var dbcontext = new AdlerContextDataContext();
            var result = dbcontext.tbl_NewsCategories.Select(x => creator(x.CategoryName, x.CategoryNo));
            return result;
        }

        public IEnumerable<tbl_New> GetNewsByCategory(int CategoryId, int Idlang)
        {
            var dbcontext = new AdlerContextDataContext();
            if (Idlang == 0)
                return dbcontext.tbl_News.Where(x => x.CategoryNo == CategoryId && x.Status == "NORMAL");
            if(Idlang==1)
                return dbcontext.tbl_News.Where(x => x.CategoryNo == CategoryId && x.Status == "NORMAL" && x.LangId == 1);
            if(Idlang==2)
                return dbcontext.tbl_News.Where(x => x.CategoryNo == CategoryId && x.Status == "NORMAL" && x.LangId == 2);
            return null;
        }

        public IEnumerable<tbl_New> GetHotNews(int Idlang)
        {
            var dbcontext = new AdlerContextDataContext();
            if(Idlang==0)
                return dbcontext.tbl_News.Where(x => x.IsHotNews==true && x.Status == "NORMAL");
            if(Idlang==1)
                return dbcontext.tbl_News.Where(x => x.IsHotNews==true && x.Status == "NORMAL" && x.LangId==1);
            if(Idlang==2)
                return dbcontext.tbl_News.Where(x => x.IsHotNews == true && x.Status == "NORMAL" && x.LangId == 2);
            return null;
        }

        public string GetTitleCategory(int CategoryId, int langId)
        {
            var dbcontext = new AdlerContextDataContext();
            if(langId==1||langId==0)
            {
                return
                dbcontext.tbl_NewsCategories.Where(x => x.CategoryNo == CategoryId).Select(x => x.CategoryName).
                    FirstOrDefault();
            }
            else if(langId==2)
            {
                var storeLang = dbcontext.tbl_StoreLanguageAtrributes
                                .Where(x => x.LangId == langId && x.IsDeleted == false && x.EntityId == CategoryId);
                return storeLang.Where(x => x.Attrribute == "Name").Select(x => x.ValueAttr).FirstOrDefault();
            }
            return null;

        }
    }
}
